package com.taptech.doufu.ui.view.topicview;
import android.app.Activity;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.CategoryPlusGsonBean;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.DFHomeNovelContentBeans;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.ui.view.topicview.adapter.NovelCategoryAdapter;
import com.taptech.doufu.ui.view.topicview.adapter.NovelCategoryTitleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicCategoryPlusView extends LinearLayout{
    private TextView tvTitle;
    private String type;
    private String pageType;
    private List<DFHomeNovelBeans> list;
    private RecyclerView rvTitle,rvContent;

    private LinearLayout layReplace;
    private ImageView ivReplace;
    private String title;
    private View layMore;

    private int MAX_COUNT = 9;
    public NovelTopicCategoryPlusView(Context context) {
        super(context);
    }

    public NovelTopicCategoryPlusView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NovelTopicCategoryPlusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private View root_layout;
    private View linearLayout0;
    private View topSpacView;
    private Context context;
    private View rl_dfnovel_layout1;
    private NovelCategoryAdapter contentAdapter;
    private HashMap<String, List<CategoryPlusGsonBean.CategoryBean.CategoryItem>> cache = new HashMap<>();
    private DFHomeNovelBeans.AlbumInfo selectAlbumInfo;
    private EventBean eventBean;

    public NovelTopicCategoryPlusView(final Context context, DFHomeNovelContentBeans model, final String type,String pageType){
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_category_plus_view, null);
        this.context = context;
        this.type = type;
        this.pageType = pageType;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        rl_dfnovel_layout1 = view.findViewById(R.id.rl_dfnovel_layout1);
        topSpacView = view.findViewById(R.id.home_dfnovel_layout_space);
        layReplace = (LinearLayout) view.findViewById(R.id.layReplace);
        layMore = view.findViewById(R.id.layMore);
        layMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectAlbumInfo != null && !TextUtils.isEmpty(selectAlbumInfo.getMoreUrl())) {
                    ChoiceUtils.jumpToMoreNovel(context,selectAlbumInfo.getName(),selectAlbumInfo.getMoreUrl());
                }
            }
        });
        eventBean = model.getEventData();
        layReplace.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                UmengEventUtil.addRightTopBtnEvent(context,eventBean,type,pageType);
                //播放动画
                Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.img_animation);
                LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
                animation.setInterpolator(lin);
                ivReplace.startAnimation(animation);
                if (cache != null && selectAlbumInfo != null && !TextUtils.isEmpty(selectAlbumInfo.getName())) {
                    //清楚缓存数据
                    cache.remove(selectAlbumInfo.getName());
                    //请求新的数据
                    getData(selectAlbumInfo);
                }

            }
        });
        ivReplace = (ImageView) view.findViewById(R.id.ivReplace);
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        rvTitle = (RecyclerView) view.findViewById(R.id.rvTitle);
        rvContent = (RecyclerView) view.findViewById(R.id.rvContent);
        tvTitle.setText(model.getTitle());
        changeDayModelBg();
        if (list != null) {
            list.clear();
        }
        list = model.getList();
        title = model.getTitle();
        MeasuredGridLayoutManager gridLayoutManager = new MeasuredGridLayoutManager(getContext(),3);
        gridLayoutManager.setScrollEnabled(false);
        NovelCategoryTitleAdapter adapter = new NovelCategoryTitleAdapter(getContext(), list, new NovelCategoryTitleAdapter.OnClickItem() {
            @Override
            public void onClick(DFHomeNovelBeans model) {
                if (model != null && model.getAlbumInfo() != null) {
                    getData(model.getAlbumInfo());
                }
            }
        });
        rvTitle.setLayoutManager(gridLayoutManager);
        rvTitle.setAdapter(adapter);
        addView(view);
    }




    private void getData(final DFHomeNovelBeans.AlbumInfo albumInfo) {
        selectAlbumInfo = albumInfo;
        //取缓存数据
        if (cache != null && cache.containsKey(albumInfo.getName())) {
            List<CategoryPlusGsonBean.CategoryBean.CategoryItem> list = cache.get(albumInfo.getName());
            if (list != null && list.size() > 0) {
                setContent(list);
                return;
            }
        }
        ApiClient.getInstance().setUseCache(false).getService()
                .getCategoryPlusData(albumInfo.getUrl())
                .compose(RxJavaHelper.<CategoryPlusGsonBean>observeOnMainThread((Activity) context))
                .subscribe(new BaseSubscriber<CategoryPlusGsonBean>(){
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        setContent(new ArrayList<CategoryPlusGsonBean.CategoryBean.CategoryItem>());
                    }

                    @Override
                    public void onNext(CategoryPlusGsonBean bean) {
                        super.onNext(bean);
                        if (bean.getData() != null && bean.getData().getList() != null) {
                            setContent(formatContent(bean.getData().getList()));
                            cache.put(albumInfo.getName(), bean.getData().getList());
                        }else{
                            setContent(new ArrayList<CategoryPlusGsonBean.CategoryBean.CategoryItem>());
                        }
                    }
                });
    }


    private List<CategoryPlusGsonBean.CategoryBean.CategoryItem> formatContent(List<CategoryPlusGsonBean.CategoryBean.CategoryItem> list) {
        return list.subList(0, list.size() > MAX_COUNT ? MAX_COUNT : list.size());
    }


    private void setContent(List<CategoryPlusGsonBean.CategoryBean.CategoryItem> list) {
        if (contentAdapter != null) {
            contentAdapter.refrush(list);
        }else{
            contentAdapter = new NovelCategoryAdapter(getContext(), list, type,title,eventBean,pageType);
            MeasuredLinearLayoutManager manager = new MeasuredLinearLayoutManager(getContext());
            manager.setScrollEnabled(false);
            rvContent.setLayoutManager(manager);
            rvContent.setAdapter(contentAdapter);
        }
    }


    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_1));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.divider_line_F1F6F9));
        rl_dfnovel_layout1.setBackgroundColor(context.getResources().getColor(R.color.white));

    }

    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        rl_dfnovel_layout1.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));

    }

}
